//
//  ChangeUkeyPasswordPlugin.m
//  PCDBankSix
//
//  Created by 冀银开发 on 2023/11/10.
//

#import "ChangeUkeyPasswordPlugin.h"
#import "PCDBluetoothManager.h"
#import "YTUkeyEnterpriseModel.h"
#import "PCDUkeyAlertView.h"
#import "YTBluetoothAuthHandle.h"
#import "YTUKeySNAlertView.h"
#import "PCDFTUkeyManager.h"
#import "IBankDevice.h"
#import "YTChangeBTPinViewController.h"

@interface ChangeUkeyPasswordPlugin ()<disconnectedDelegate>

/// PIN码弹窗
@property (strong ,nonatomic)PCDUkeyAlertView *ukAlertView;
@property (nonatomic,copy) NSString * PIN;
@property (nonatomic,copy) NSString * strCallback;
@property (nonatomic,copy) NSString * failCallback;
@property (nonatomic,copy) NSString * ukeyCodeType;
@property (nonatomic,copy) NSString * SN;
@property (nonatomic,copy) NSString * businessText;
@property (nonatomic,strong) PCDBankWebViewController * vc;

@end
@implementation ChangeUkeyPasswordPlugin

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback{
    
    NSDictionary *dictData = (NSDictionary *)data;
    self.vc = context.viewController;
    self.strCallback = dictData[@"success"];
    self.failCallback = dictData[@"fail"];
    self.ukeyCodeType = dictData[@"ukeyCodeType"];
    self.SN = dictData[@"SN"];
    
    Weakify(self);
    //获取蓝牙权限
    [YTBluetoothAuthHandle.sharedInstance requestBluetoothRequest:^(BOOL granted, CBManagerState status) {
        if (granted) {
            [wself connectUKey];
        }else {
            evaluateJavaScriptOnMainThread(wself, wself.failCallback, [@{@"errCode":@""} JSONRepresentation]);
            if (status == CBManagerStateUnauthorized) {
                [PCDCommonTools showBluetoothAuthAlert];
            }
        }
    }];
}

- (void)connectUKey {
    Weakify(self);
    if ([self.ukeyCodeType integerValue] == 1) {
        //飞天
        YTChangeBTPinViewController * vc = [[YTChangeBTPinViewController alloc]init];
        vc.SN = self.SN;
        vc.modifyResultBlock = ^(BOOL success, NSString * _Nonnull info) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!success){
                    evaluateJavaScriptOnMainThread(wself, wself.failCallback, @"");
                    return;
                }
                [wself.vc.navigationController popViewControllerAnimated:YES];
                evaluateJavaScriptOnMainThread(wself, wself.strCallback, @"");
            });
        };
        [self.vc.navigationController pushViewController:vc animated:YES];
    }else if ([self.ukeyCodeType integerValue] == 2) {
        //文鼎
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(wd_didDisconnectUkey:) name:ESIBankDeviceDidDisconnect object:nil];
        [self.vc.view showHBWaitWithTitle:@"蓝牙key连接中..." isFullScreen:YES];
        [PCDBluetoothManager.shareManager connectUKeyWithSN:wself.SN connectBlock:^(BOOL result, id  _Nonnull model) {
            [wself.vc.view showHBWait:NO isFullScreen:YES];
            if (result) {///连接成功
                YTBluetoothAuthHandle.sharedInstance.ukeyType = YTUkeyConnectType_WD;
                [wself.vc.view showHBWait:YES isFullScreen:YES];
                [PCDBluetoothManager.shareManager UIchangePINCodeWithCompleteBlock:^(BOOL result, NSString * _Nonnull errorString) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [wself.vc.view showHBWait:NO isFullScreen:YES];
                        [PCDUtil toastMessage:errorString];
                        if (result) {
                            evaluateJavaScriptOnMainThread(wself, wself.strCallback, @"");
                        }else {
                            evaluateJavaScriptOnMainThread(wself, wself.failCallback, @"");
                        }
                    });
                }];
            } else {/// 连接失败
                dispatch_async(dispatch_get_main_queue(), ^{
                    evaluateJavaScriptOnMainThread(wself, wself.failCallback, @"");
                });
            }
        }];
    }
}



- (void)wd_didDisconnectUkey:(NSNotification *)noti{
    Weakify(self);
    dispatch_async(dispatch_get_main_queue(), ^{
        evaluateJavaScriptOnMainThread(wself, wself.failCallback, [@{@"errCode":@"蓝牙key已经断开连接"} JSONRepresentation]);
    });
}

-(void)FTdisconnectedAction {
    Weakify(self);
    dispatch_async(dispatch_get_main_queue(), ^{
        evaluateJavaScriptOnMainThread(wself, wself.failCallback, [@{@"errCode":@"蓝牙key已经断开连接"} JSONRepresentation]);
    });
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
